#!/bin/sh
####  此脚本应该在command目录下执行

### 为了在任意路径执行该脚本，加入以下代码

WORK_PATH=$(pwd)
BASH_PATH=$(cd `dirname $0`; pwd)

##########################################

if [ ! -d "$BASH_PATH/../lib" -o ! -d "$BASH_PATH/../build" ];then
   echo "dependent dir don\`t exist!"
   exit
fi

BIN="$BASH_PATH/cat.bin"
BIN_NAME="$BASH_PATH/cat"
CFLAGS="-Wall -c -fno-builtin -W  -fno-stack-protector"
LIBS="-I $BASH_PATH/../lib/ -I $BASH_PATH/../lib/kernel/ -I $BASH_PATH/../lib/user/ -I \
      $BASH_PATH/../kernel/ -I $BASH_PATH/../device/ -I $BASH_PATH/../thread/ -I \
      $BASH_PATH/../userprog/ -I $BASH_PATH/../fs/ -I $BASH_PATH/../shell/"
OBJS="$BASH_PATH/../build/string.o $BASH_PATH/../build/syscall.o \
      $BASH_PATH/../build/stdio.o $BASH_PATH/../build/assert.o $BASH_PATH/start.o"
DD_IN=$BIN
DD_OUT="$BASH_PATH/../hd50M.img" 

nasm -f elf32 $BASH_PATH/start.S -o $BASH_PATH/start.o
ar rcs $BASH_PATH/simple_crt.a $OBJS $BASH_PATH/start.o

gcc -m32 $CFLAGS  $LIBS -o  $BIN_NAME".o" $BIN_NAME".c"
ld  -melf_i386  $BIN_NAME".o" $BASH_PATH/simple_crt.a -o $BIN
SEC_CNT=$(ls -l $BIN|awk '{printf("%d", ($5+511)/512)}')

if [ -f $BIN ];then
   dd if=$DD_IN of=$DD_OUT bs=512 count=$SEC_CNT seek=300 conv=notrunc
fi
